﻿using KAsFrame.Common.Attr;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace KAsFrame.Common.ServiceExtentisons
{
    public static class SqlSugarExtensions
    {
        /// <summary>
        /// 添加sqlsugar服务
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddSqlSugar(this IServiceCollection services, IConfiguration configuration)
        {
            var sqlSugar = new SqlSugarScope(
                new ConnectionConfig()
                {
                    // 注意使用数据库类型
                    //DbType = DbType.SqlServer,
                    DbType = DbType.MySql,
                    ConnectionString = configuration.GetConnectionString("Default"),
                    IsAutoCloseConnection = true,
                },
                db =>
                {
                    //db.QueryFilter.Add(new SqlFilterItem()
                    //{

                    //});
                    //单例参数配置，所有上下文生效
                    // sql执行前
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        //Console.WriteLine(sql);//输出sql
                    };
                    // 发生异常时
                    db.Aop.OnError = ex =>
                    {
                        // 输出日志
                    };
                    // 差异审计
                    db.Aop.OnDiffLogEvent = diffEvent =>
                    {
                        //只有添加自动审计特性的实体才会处理自动审计过程
                        var bizType = diffEvent.BusinessData.GetType();
                        if (bizType.GetCustomAttribute<AutoAuditAttribute>() != null)
                        {
                            // 记录数据变化
                        }
                    };
                    // 数据过滤器，用于添加默认值
                    db.Aop.DataExecuting = (oldValue, entityInfo) =>
                    {
                        //inset生效
                        if (entityInfo.PropertyName == "CreateTime" && entityInfo.OperationType == DataFilterType.InsertByObject)
                        {
                            entityInfo.SetValue(DateTime.Now);//修改CreateTime字段
                        }
                        //update生效        
                        if (entityInfo.PropertyName == "UpdateTime" && entityInfo.OperationType == DataFilterType.UpdateByObject)
                        {
                            entityInfo.SetValue(DateTime.Now);//修改UpdateTime字段
                        }

                        //根据当前列修改另一列 可以么写
                        //if(当前列逻辑==XXX)
                        //var properyDate = entityInfo.EntityValue.GetType().GetProperty("Date");
                        //if(properyDate!=null)
                        //properyDate.SetValue(entityInfo.EntityValue,1);
                    };
                });
            services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
        }
    }
}
